package com.ebeitech.auction.bean;

import com.ebeitech.library.constant.SPConstants;
import com.ebeitech.library.util.SPManager;
import com.ebeitech.library.util.StringUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by alice on 2017/11/1.
 */
public class Auction implements Serializable {

	/**
	 * 房间Id
	 */
	private String auctionId;

	/**
	 * 房间号
	 */
	private String auctionCode;

	/**
	 * 竞拍时间
	 */
	private String auctionStartTime;

	/**
	 * 竞拍时间
	 */
	private String auctionEndTime;

	/**
	 * 地块名称
	 */
	private String blockName;

	/**
	 * 地块位置
	 */
	private String blockLocation;

	/**
	 * 跟进区域
	 */
	private String area;

	/**
	 * 创建人Id
	 */
	private String creatorId;

	/**
	 * 创建人名字
	 */
	private String creatorName;

	/**
	 * 创建时间
	 */
	private String createTime;

	/**
	 * 状态code
	 * 1:未发布，2:已发布，3:已结束，4:已关闭，5:已开始
	 */
	private String stateCode;

	/**
	 * 状态text
	 */
	private String stateText;

	/**
	 * 是否本方获取
	 */
	private boolean isGetted;

	/**
	 * 是否放弃
	 */
	private boolean isGiveup;

	/**
	 * 地块信息
	 */
	private LandInfo landInfo;

	/**
	 * 各阶段
	 */
	private List<AuctionPhase> auctionPhases;

	/**
	 * 各竞价人
	 */
	private List<AuctionBidder> auctionBidders;

	private List<AuctionUser> auctionUsers;

	public String getAuctionId() {
		return auctionId;
	}

	public void setAuctionId(String auctionId) {
		this.auctionId = auctionId;
	}

	public String getAuctionCode() {
		return auctionCode;
	}

	public void setAuctionCode(String auctionCode) {
		this.auctionCode = auctionCode;
	}

	public String getAuctionStartTime() {
		return auctionStartTime;
	}

	public void setAuctionStartTime(String auctionStartTime) {
		this.auctionStartTime = auctionStartTime;
	}

	public String getAuctionEndTime() {
		return auctionEndTime;
	}

	public void setAuctionEndTime(String auctionEndTime) {
		this.auctionEndTime = auctionEndTime;
	}

	public String getBlockName() {
		return blockName;
	}

	public void setBlockName(String blockName) {
		this.blockName = blockName;
	}

	public String getBlockLocation() {
		return blockLocation;
	}

	public void setBlockLocation(String blockLocation) {
		this.blockLocation = blockLocation;
	}

	public String getArea() {
		return area;
	}

	public void setArea(String area) {
		this.area = area;
	}

	public String getCreatorId() {
		return creatorId;
	}

	public void setCreatorId(String creatorId) {
		this.creatorId = creatorId;
	}

	public String getCreatorName() {
		return creatorName;
	}

	public void setCreatorName(String creatorName) {
		this.creatorName = creatorName;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getStateCode() {
		return stateCode;
	}

	public void setStateCode(String stateCode) {
		this.stateCode = stateCode;
	}

	public String getStateText() {
		return stateText;
	}

	public void setStateText(String stateText) {
		this.stateText = stateText;
	}

	public boolean getIsGetted() {
		return isGetted;
	}

	public void setIsGetted(boolean isGetted) {
		this.isGetted = isGetted;
	}

	public boolean getIsGiveup() {
		return isGiveup;
	}

	public void setIsGiveup(boolean isGiveup) {
		this.isGiveup = isGiveup;
	}

	public LandInfo getLandInfo() {
		return landInfo;
	}

	public void setLandInfo(LandInfo landInfo) {
		this.landInfo = landInfo;
	}

	public List<AuctionPhase> getAuctionPhases() {
		if (auctionPhases == null) {
			auctionPhases = new ArrayList<AuctionPhase>();
		}
		Collections.sort(auctionPhases, new Comparator<AuctionPhase>() {
			@Override
			public int compare(AuctionPhase lhs, AuctionPhase rhs) {
				return lhs.getPhaseSort() - rhs.getPhaseSort();
			}
		});
		return auctionPhases;
	}

	public void setAuctionPhases(List<AuctionPhase> auctionPhases) {
		this.auctionPhases = auctionPhases;
	}

	public List<AuctionBidder> getAuctionBidders() {
		if (auctionBidders == null) {
			auctionBidders = new ArrayList<AuctionBidder>();
		}
		return auctionBidders;
	}

	public void setAuctionBidders(List<AuctionBidder> auctionBidders) {
		this.auctionBidders = auctionBidders;
	}

	public List<AuctionUser> getAuctionUsers() {
		if (auctionUsers == null) {
			auctionUsers = new ArrayList<AuctionUser>();
		}
		return auctionUsers;
	}

	public void setAuctionUsers(List<AuctionUser> auctionUsers) {
		this.auctionUsers = auctionUsers;
	}

	/**
	 * 获取最新的阶段
	 * @return
	 */
	public AuctionPhase getCurrentAuctionPhase() {
		if (auctionPhases == null || auctionPhases.size() == 0) {
			return null;
		}
		for (AuctionPhase auctionPhase : auctionPhases) {
			if (AuctionPhase.PHASE_STATE_BEGINNING.equals(auctionPhase.getPhaseState())) {
				return auctionPhase;
			}
		}
		return null;
	}

	/**
	 * 获取最后的阶段
	 * @return
	 */
	public AuctionPhase getFinalAuctionPhase() {
		if (auctionPhases == null || auctionPhases.size() == 0) {
			return null;
		}
		int sort = 0;
		for (AuctionPhase auctionPhase : auctionPhases) {
			if (sort <= auctionPhase.getPhaseSort()) {
				sort = auctionPhase.getPhaseSort();
			}
		}
		return auctionPhases.get(sort);
	}

	/**
	 * 根据Code获取竞价人
	 * @param bidderCode
	 * @return
	 */
	public AuctionBidder getAuctionBidderByCode(String bidderCode) {
		if (auctionBidders == null || auctionBidders.size() == 0) {
			return null;
		}
		for (AuctionBidder auctionBidder : auctionBidders) {
			if (bidderCode.equals(auctionBidder.getBidderAuctionCode())) {
				return auctionBidder;
			}
		}
		return null;
	}

	/**
	 * 获取本人的竞价人信息
	 * @return
	 */
	public AuctionBidder getOurAuctionBidder() {
		if (auctionBidders == null || auctionBidders.size() == 0) {
			return null;
		}
		for (AuctionBidder auctionBidder : auctionBidders) {
			if (auctionBidder.getIsUs()) {
				return auctionBidder;
			}
		}
		return null;
	}

	/**
	 * 获取本人的用户信息,主要是权限
	 * @return
	 */
	public AuctionUser getCurrentAuctionUser() {
		if (auctionUsers == null || auctionUsers.size() == 0) {
			return null;
		}
		for (AuctionUser auctionUser : auctionUsers) {
			if (SPManager.getInstance().get(SPConstants.USER_ID, "").equals(auctionUser.getUserId())) {
				return auctionUser;
			}
		}
		return null;
	}

	@Override
	public boolean equals(Object o) {
		if (!(o instanceof Auction)) {
			return false;
		}
		return StringUtil.getDefaultString(auctionId).equals(((Auction) o).getAuctionId());
	}
}
